﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace LanguageAnalyse
{
    public partial class TranslateSuggest : Form
    {
        public TranslateSuggest()
        {
            InitializeComponent();
        }
        List<LangItem> LangItemListCN = new List<LangItem>();
        List<LangItem> LangItemListTW = new List<LangItem>();
        List<LangItem> LangItemListEN = new List<LangItem>();
        private void btnOpen_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();
            txtPath.Text = folderBrowserDialog1.SelectedPath;
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            DirectoryInfo dir = new DirectoryInfo(txtPath.Text);
            if (!dir.Exists)
            {
                MessageBox.Show("目錄不存在！");
                return;
            }

            foreach (FileInfo file in dir.GetFiles())
            {
                var LanguageItemList = getLanguageItem(file.Name);
                PublicClass.LoadLanguage(LanguageItemList, file.FullName);
            }
            MessageBox.Show("裝載完成！");
        }

        private List<LangItem> getLanguageItem(string key)
        {
            switch (key)
            {
                case "簡體":
                case "zh-cn.xml":
                    return LangItemListCN;
                case "繁體":
                case "zh-tw.xml":
                    return LangItemListTW;
                case "英文":
                case "en-us.xml":
                    return LangItemListEN;
            }
            return null;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            var lang1 = from aa in LangItemListCN
                    where aa.lgvalue.Contains(txtword.Text.Trim())
                    select aa;


            var lang2= from aa in lang1 
                       join bb in LangItemListEN on aa.lgkey equals  bb.lgkey
                       select bb;

            var q = from p in lang2
                    group p by p.lgvalue into g
                    orderby g.Count() descending 
                    select new {Key=g.Key,Count= g.Count()};
 
            gridEN.DataSource = q.ToList();

 
        }

        private void SearchLanguage_Load(object sender, EventArgs e)
        {

        }
    }
}
